package com.yang.dao;

import com.yang.pojo.BasicInfo;
import com.yang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;

public class BasicInfoDao {
    //查询所有评估类目的信息
    public ArrayList<BasicInfo> selectBasicInfo() throws SQLException {
        ArrayList<BasicInfo> basicInfos = new ArrayList<>();
        String sql = "SELECT basic_info_id basicInfoId,basic_info_name basicInfoName,basic_info_status basicInfoStatus FROM tb_basic_info";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        basicInfos = (ArrayList<BasicInfo>) queryRunner.query(sql, new BeanListHandler<BasicInfo>(BasicInfo.class));
        return basicInfos;
    }

    //添加评估类目信息
    public int insertBasicInfo(String basicInfoName) throws SQLException {
        int basicInfoId = 0;
        String sql = "INSERT INTO tb_basic_info(basic_info_name,basic_info_status) VALUES(?,1)";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        Object object = queryRunner.insert(sql, new ScalarHandler<>(), basicInfoName);
        basicInfoId = Integer.parseInt(String.valueOf(object));
        return basicInfoId;
    }

    //根据id修改状态
    public int changeStatusById(int basicInfoStatus,int basicInfoId) throws SQLException {
        int i = 0;
        String sql = "UPDATE tb_basic_info SET basic_info_status=? WHERE basic_info_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql,  basicInfoStatus,basicInfoId);
        return i;
    }

    //根据id删除BasicInfo信息
    public int DeleteBasicInfoById(int basicInfoId) throws SQLException {
        int i=0;
        String sql = "DELETE FROM tb_basic_info WHERE basic_info_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql, basicInfoId);
        return i;

    }

    //根据id修改类目名称
    public int updateBasicInfoById(String basicInfoName,int basicInfoId) throws SQLException {
        int i = 0;
        String sql = "UPDATE tb_basic_info SET basic_info_name=? WHERE basic_info_id=?";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        i = queryRunner.update(sql, basicInfoName, basicInfoId);
        return i;
    }
    //根据id查询类目
    public BasicInfo selectBasicInfoById(int basicInfoId) throws SQLException {
        String sql = "SELECT basic_info_id basicInfoId,basic_info_name basicInfoName,basic_info_status basicInfoStatus FROM tb_basic_info WHERE basic_info_id = ?;";
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        BasicInfo basicInfo = queryRunner.query(sql,new BeanHandler<BasicInfo>(BasicInfo.class),basicInfoId);
        return basicInfo;
    }

}
